package com.ccxc.question.exam.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.ccxc.question.exam.dto.QuestionOptionDTO;


@Component
public class QuestionOptionDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public int add(final String testId, final List<QuestionOptionDTO> dto) {
        final String sql = "INSERT INTO  t_question_option (OPTION_CONTENT, `OPTION`, QUESTION_ID) VALUES (?, ?, ?)";

        //批量添加
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1, dto.get(i).getContent());
                ps.setString(2, dto.get(i).getOption());
                ps.setString(3, testId);
            }

            public int getBatchSize() {
                return dto.size();
            }
        });
        return dto.size();
    }

    public List<QuestionOptionDTO> findByTestId2(String queid) {
        final String sql = "SELECT QUESTION_ID, OPTION_CONTENT, `OPTION` FROM  t_question_option WHERE QUESTION_ID = ?";
        return jdbcTemplate.query(sql,
                new Object[]{queid},
                new int[]{Types.VARCHAR},
                (ResultSet rs, int rowNum) -> new QuestionOptionDTO(rs.getLong("QUESTION_ID"), rs.getString("OPTION"), rs.getString("OPTION_CONTENT"))
        );
    }

    public List<QuestionOptionDTO> findByTestId(String testId) {
        final String sql = "SELECT QUESTION_ID, OPTION_CONTENT, `OPTION` FROM  t_question_option WHERE QUESTION_ID = ?";
        return jdbcTemplate.query(sql,
                new Object[]{testId},
                new int[]{Types.VARCHAR},
                (ResultSet rs, int rowNum) -> new QuestionOptionDTO(rs.getLong("QUESTION_ID"), rs.getString("OPTION"), rs.getString("OPTION_CONTENT"))
        );
    }

    public int update(String testId, final List<QuestionOptionDTO> dtos) {
        final String sql = "UPDATE  t_question_option SET OPTION_CONTENT = ?, `OPTION` = ?, QUESTION_ID = ? WHERE ID = ?";

        //批量修改
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1, dtos.get(i).getContent());
                ps.setString(2, dtos.get(i).getOption());
                ps.setString(3, testId);
                ps.setLong(4, dtos.get(i).getId());
            }

            @Override
            public int getBatchSize() {
                return dtos.size();
            }
        });
        return dtos.size();
    }

    public int delete(String testId) {
        final String sql = "DELETE FROM  t_question_option WHERE QUESTION_ID = ?";
        return jdbcTemplate.update(sql,
                new Object[]{testId},
                new int[]{Types.VARCHAR}
        );
    }
}